<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" errorPage="/error.jsp" isELIgnored="false" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="atleap" %>
<html:xhtml />

<atleap:constants className="com.blandware.atleap.webapp.util.core.WebappConstants" var="GRID_PAGE_SIZE" />
<atleap:globalProperty name="${pageScope.GRID_PAGE_SIZE}" defaultValue="10" type="integer" var="pageSize" />

<atleap:isUserInRole roles="forum-topic-delete" var="canDelete" />

<jsp:include page="/WEB-INF/pages/forum/pathFragment.jsp" />

<c:set var="infoCells" value="5" />
<c:set var="constantButtonCells" value="1" />
<c:set var="additionalCells" value="0" />
<c:if test="${canDelete}">
    <c:set var="additionalCells" value="${additionalCells+1}" />
</c:if>

<atleap:messages />
<atleap:grid name="topicGrid" pageSize="${pageSize}">
    <atleap:queryParameter name="forumId" value="${currentForum.id}" />
    <table border="0" cellpadding="0" cellspacing="0" class="grid" id="topicList">
        <thead>
            <tr>
                <th nowrap="nowrap" width="40%">
                    <atleap:column fieldName="title.simpleValue" fieldKey="forum.post.form.subject">
                        <atleap:message key="${fieldKey}" />
                        <atleap:stringFilter>
                            <atleap:filterEnabled>
                                <img border="0" src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/grid/filterEnabled.gif" />"  />
                            </atleap:filterEnabled>
                            <atleap:filterDisabled>
                                <img border="0" src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/grid/filterDisabled.gif" />"  />
                            </atleap:filterDisabled>
                        </atleap:stringFilter>
                    </atleap:column>
                </th>
                <th nowrap="nowrap">
                    <atleap:column fieldName="topic.firstPost.author" fieldKey="forum.topic.form.author">
                        <atleap:message key="${fieldKey}" />
                    </atleap:column>
                </th>
                <th nowrap="nowrap">
                    <atleap:message key="forum.topic.form.dateCreated" />
                </th>
                <th nowrap="nowrap">
                    <atleap:message key="forum.topic.form.numberOfReplies" />
                </th>
                <th nowrap="nowrap">
                    <atleap:message key="forum.topic.form.lastReply" />
                </th>
                <c:if test="${constantButtonCells + additionalCells > 0}">
                    <th colspan="${constantButtonCells + additionalCells}">
                        <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/spacer.gif" />" border="0"  />
                    </th>
                </c:if>
             </tr>
        </thead>
        <tbody>
            <atleap:contextMenu var="menuId">
                <atleap:message key="forum.topic.form.readTopic" var="readTopicMsg" />
                <atleap:message key="forum.post.form.readPost" var="readPostMsg" />
                <atleap:rowsIterator beanId="forumManager" method="listTopics" var="topic" oddClass="oddRow" evenClass="evenRow">
                    <atleap:itemSet>
                        <atleap:rewriteUrl action="forum/topic${topic.id}" var="defaultUrl" />
                        <c:set var="onTrClickCode" value="" />
                        <c:set var="tdClass" value="defaultCursorCell" />
                        <c:set var="onTrClickCode" value="dispatchGridTrClick(event, this, &#039;${defaultUrl}&#039;);" />
                        <c:set var="tdClass" value="pointerCursorCell" />
                        <tr class="${rowClass}" oncontextmenu="return showContextMenu(${menuId}, event, ${indexId});" onclick="${onTrClickCode}">
                            <td class="${tdClass}">
                                <atleap:link action="forum/topic${topic.id}" transaction="false" title="${readTopicMsg}">
                                    <atleap:fieldValue valueMap="${topic.firstPost.title}" complexSearch="true" filter="true" />
                                </atleap:link>
                            </td>
                            <td class="${tdClass}">
                                <c:out value="${topic.firstPost.author}" escapeXml="true" />
                            </td>
                            <td class="${tdClass}">
                                <atleap:formatDate value="${topic.firstPost.date}" type="datetime" />
                            </td>
                            <td class="${tdClass}" align="center">
                                <c:out value="${topic.numberOfReplies}" escapeXml="true" />
                            </td>
                            <td class="${tdClass}">
                                <c:if test="${topic.lastReply != null}">
                                    <atleap:link action="forum/post${topic.lastReply.id}" title="${readPostMsg}"><atleap:fieldValue valueMap="${topic.lastReply.title}" complexSearch="true" filter="true" /></atleap:link>
                                    (<c:out value="${topic.lastReply.author}" escapeXml="true" />)<br />
                                    <atleap:formatDate value="${topic.lastReply.date}" type="datetime" />
                                </c:if>
                            </td>
                            <c:choose>
                                <c:when test="${pageContext.request.remoteUser == null || !topic.usersSubscribed[pageContext.request.remoteUser]}">
                                    <atleap:item>
                                        <td width="20" height="20" align="center" valign="middle" class="defaultCursorCell">
                                            <c:set var="trackAction" value="forum/subscription/callCreate" />
                                            <c:set var="trackTransaction" value="false" />
                                            <c:if test="${pageContext.request.remoteUser != null}">
                                                <c:set var="trackAction" value="forum/subscription/create" />
                                                <c:set var="trackTransaction" value="true" />
                                            </c:if>
                                            <atleap:itemEnabled>
                                                <atleap:message key="forum.topic.buttons.track" var="trackTitle" />
                                                <atleap:itemLink action="${trackAction}" transaction="${trackTransaction}" title="${trackTitle}">
                                                    <atleap:param name="topicId" value="${topic.id}" />
                                                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/look.gif" />" border="0" title="${trackTitle}" alt="${trackTitle}" align="absmiddle" />
                                                    <atleap:itemLabel>
                                                        ${trackTitle}
                                                    </atleap:itemLabel>
                                                </atleap:itemLink>
                                            </atleap:itemEnabled>
                                        </td>
                                    </atleap:item>
                                </c:when>
                                <c:otherwise>
                                    <atleap:item>
                                        <td width="20" height="20" align="center" valign="middle" class="defaultCursorCell">
                                            <atleap:itemEnabled>
                                                <atleap:message key="forum.topic.buttons.untrack" var="untrackTitle" />
                                                <atleap:itemLink action="forum/subscription/delete" transaction="true" title="${untrackTitle}">
                                                    <atleap:param name="topicId" value="${topic.id}" />
                                                    <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/no_look.gif" />" border="0" title="${untrackTitle}" alt="${untrackTitle}" align="absmiddle" />
                                                    <atleap:itemLabel>
                                                        ${untrackTitle}
                                                    </atleap:itemLabel>
                                                </atleap:itemLink>
                                            </atleap:itemEnabled>
                                        </td>
                                    </atleap:item>
                                </c:otherwise>
                            </c:choose>
                            <atleap:item includeInContextMenu="${canDelete}" includeInPage="${canDelete}">
                                <atleap:message key="core.commons.buttons.delete" var="deleteTitle" />
                                <td width="20" height="20" align="center" valign="middle" class="defaultCursorCell">
                                    <atleap:itemEnabled>
                                        <atleap:itemLink action="forum/topic/delete" transaction="true" confirmExpression="confirmDelete();" title="${deleteTitle}">
                                            <atleap:param name="id" value="${topic.id}" />
                                                <img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/delete.gif" />" border="0" title="${deleteTitle}" alt="${deleteTitle}" align="absmiddle" />
                                                <atleap:itemLabel>
                                                    ${deleteTitle}
                                                </atleap:itemLabel>
                                        </atleap:itemLink>
                                    </atleap:itemEnabled>
                                </td>
                            </atleap:item>
                        </tr>
                    </atleap:itemSet>
                </atleap:rowsIterator>
            </atleap:contextMenu>
            <atleap:noRows>
                <tr>
                    <td class="tfoot" colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                       <em><atleap:message key="core.commons.nothingToShow"/></em>
                    </td>
                </tr>
            </atleap:noRows>
        </tbody>
        <tfoot>
            <tr>
                <atleap:pager pageCount="10">
                    <td colspan="${infoCells + constantButtonCells + additionalCells}" align="center">
                        <atleap:message key="core.grid.pager.title" />&nbsp;
                        <atleap:firstPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/first.gif" />" border="0" alt="<atleap:message key="core.grid.pager.first"/>" title="<atleap:message key="core.grid.pager.first"/>" /></atleap:firstPage>
                        <atleap:prevPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/prev.gif" />" border="0" alt="<atleap:message key="core.grid.pager.prev"/>" title="<atleap:message key="core.grid.pager.prev"/>" /></atleap:prevPage>
                        <atleap:pagesIterator>
                            <atleap:page>${pageNumber}&nbsp;</atleap:page>
                            <atleap:currentPage><b>${pageNumber}</b>&nbsp;</atleap:currentPage>
                        </atleap:pagesIterator>
                        <atleap:nextPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/next.gif" />" border="0" alt="<atleap:message key="core.grid.pager.next"/>" title="<atleap:message key="core.grid.pager.next"/>" /></atleap:nextPage>
                        <atleap:lastPage><img src="<atleap:rewriteUrl href="/images/theme/${appConfig['frontendCssTheme']}/last.gif" />" border="0" alt="<atleap:message key="core.grid.pager.last"/>" title="<atleap:message key="core.grid.pager.last"/>" /></atleap:lastPage>
                    </td>
                </atleap:pager>
            </tr>
        </tfoot>
    </table>
    <atleap:form action="forum/topic/callCreate" method="POST" validateOnSubmit="false">
        <atleap:submit styleClass="button"><atleap:message key="forum.topic.buttons.create" /></atleap:submit>
    </atleap:form>
</atleap:grid>
